package com.sphd.restaurantsystem.activitys.marking;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.Button;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.info.LoginInfo;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.presenter.AddCzInfoPresenterImpl;
import com.sphd.restaurantsystem.utils.Constant;
import com.sphd.restaurantsystem.view.AddCzInfoView;
import com.wzh.frame.utils.StringUtils;

import org.litepal.crud.DataSupport;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddCzInfoActivity extends BaseActivity implements AddCzInfoView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.money)
    MaterialEditText money;
    @BindView(R.id.cz_money)
    MaterialEditText czMoney;
    @BindView(R.id.update)
    Button update;
    Intent intent;
    Bundle bundle;
    RestaurantInfo restaurantInfo;
    LoginInfo loginInfo;
    AddCzInfoPresenterImpl addCzPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_cz_info);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(view -> finish());
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        loginInfo = DataSupport.findLast(LoginInfo.class);
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        addCzPresenter = new AddCzInfoPresenterImpl(this);
        setTitleBar(toolbar,"充值设置");
    }

    @OnClick(R.id.update)
    public void onClick() {
        if (StringUtils.isEmpty(money.getText().toString()) || StringUtils.isEmpty(czMoney.getText().toString())) {
            toast("缴存金额与充值金额不能为空");
        } else {
            addData();
        }


    }

    private void addData() {
        addCzPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.AddCzList + "cgid=" + restaurantInfo.getId()
                + "&receiptmoney=" + money.getText().toString().trim() + "&addmoney=" + czMoney.getText().toString().trim());
    }

    @Override
    public void recRes(int res) {
        if (res==1){
            toast("编辑成功");
            setResult(0);
            finish();
        }else {
            toast("编辑失败，请重试！");

        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
